﻿using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Library.WebControls.SimplePaging {
	public sealed class LeftCellPaging : TableCell {

		private class DeleteIcon : IconControl {
			[DefaultValue("~/style/simple/images/delete.png")]
			public override string ImageUrl {
				get {
					return base.ImageUrl;
				}
				set {
					base.ImageUrl = value;
				}
			}
			[DefaultValue(false)]
			public override bool Visible {
				get {
					return base.Visible;
				}
				set {
					base.Visible = value;
				}
			}
		}
		private class RefreshIcon : IconControl {
			[DefaultValue("~/style/simple/images/refresh.png")]
			public override string ImageUrl {
				get {
					return base.ImageUrl;
				}
				set {
					base.ImageUrl = value;
				}
			}

		}
		private class EditIcon : IconControl {
			[DefaultValue("~/style/simple/images/edit.png")]
			public override string ImageUrl {
				get {
					return base.ImageUrl;
				}
				set {
					base.ImageUrl = value;
				}
			}
			[DefaultValue(false)]
			public override bool Visible {
				get {
					return base.Visible;
				}
				set {
					base.Visible = value;
				}
			}
		}
		private class NewIcon : IconControl {
			[DefaultValue("~/style/simple/images/new.png")]
			public override string ImageUrl {
				get {
					return base.ImageUrl;
				}
				set {
					base.ImageUrl = value;
				}
			}
			[DefaultValue(false)]
			public override bool Visible {
				get {
					return base.Visible;
				}
				set {
					base.Visible = value;
				}
			}
		}
		private class SaveIcon : IconControl {
			[DefaultValue("~/style/simple/images/save.png")]
			public override string ImageUrl {
				get {
					return base.ImageUrl;
				}
				set {
					base.ImageUrl = value;
				}
			}
			[DefaultValue(false)]
			public override bool Visible {
				get {
					return base.Visible;
				}
				set {
					base.Visible = value;
				}
			}
		}
		private class CancelIcon : IconControl {
			[DefaultValue("~/style/simple/images/canceledit.png")]
			public override string ImageUrl {
				get {
					return base.ImageUrl;
				}
				set {
					base.ImageUrl = value;
				}
			}
			[DefaultValue(false)]
			public override bool Visible {
				get {
					return base.Visible;
				}
				set {
					base.Visible = value;
				}
			}
		}
		private readonly IconControl _cancel;
		private readonly IconControl _delete;
		private readonly IconControl _edit;
		private readonly IconControl _new;
		private readonly IconControl _refresh;
		private readonly IconControl _save;

		private Table _leftTable;

		public LeftCellPaging() {
			_leftTable = new Table();
			_leftTable.CellPadding = 0;
			_leftTable.CellSpacing = 0;
			_leftTable.BorderWidth = 0;
			_leftTable.Rows.Add(new TableRow());
			Controls.Add(_leftTable);

			_refresh = new RefreshIcon { ImageUrl = "~/style/simple/images/refresh.png" };
			_delete = new DeleteIcon { ImageUrl = "~/style/simple/images/delete.png", Visible = false };
			_edit = new EditIcon { ImageUrl = "~/style/simple/images/edit.png", Visible = false };
			_new = new NewIcon { ImageUrl = "~/style/simple/images/new.png", Visible = false };
			_save = new SaveIcon { ImageUrl = "~/style/simple/images/save.png", Visible = false };
			_cancel = new CancelIcon { ImageUrl = "~/style/simple/images/canceledit.png", Visible = false };
			CssClass = "left-cell-paging";
			HorizontalAlign = HorizontalAlign.Left;
		}
		[DefaultValue(HorizontalAlign.Left)]
		public override HorizontalAlign HorizontalAlign {
			get {
				return base.HorizontalAlign;
			}
			set {
				base.HorizontalAlign = value;
			}
		}
		[DefaultValue("left-cell-paging")]
		public override string CssClass {
			get {
				return base.CssClass;
			}
			set {
				base.CssClass = value;
			}
		}

		public IconControl Refresh {
			get { return _refresh; }
		}

		public IconControl Delete {
			get { return _delete; }
		}

		public IconControl Edit {
			get { return _edit; }
		}

		public IconControl New {
			get { return _new; }
		}

		public IconControl Save {
			get { return _save; }
		}

		public IconControl Cancel {
			get { return _cancel; }
		}
		//private void AddControl(int index, Control ctr) {
		//  var div = new HtmlGenericControl("div");
		//  div.Attributes["class"] = "sppaging-icon";
		//  div.Controls.Add(ctr);
		//  var cell = new TableCell();
		//  cell.Controls.Add(div);
		//  cell.VerticalAlign = VerticalAlign.Middle;
		//  _leftTable.Rows[0].Cells.Add(cell);

		//  //Controls.AddAt(index, div);
		//}

		public void AddIconControl(Image iconControl) {
			if (!iconControl.Visible) return;
			var div = new HtmlGenericControl("div");
			div.Attributes["class"] = "sppaging-icon";
			div.Controls.Add(iconControl);
			var cell = new TableCell();
			cell.Controls.Add(div);
			cell.VerticalAlign = VerticalAlign.Middle;
			_leftTable.Rows[0].Cells.Add(cell);

			//Controls.AddAt(index, div);
		}

		protected override void Render(HtmlTextWriter writer) {

			AddIconControl(_refresh);
			AddIconControl(_save);
			AddIconControl(_cancel);
			AddIconControl(_edit);
			AddIconControl(_new);
			AddIconControl(_delete);
			base.Render(writer);
		}
	}
}